import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/process_nodes_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/train_program/train_program_controller.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/education/me_train_program/train_ing_program_controller.dart';
import 'package:get/get.dart';
///在线培训
class OnlineTrainingController extends GetxController {
  /// 审核意见
  String approvalOpinions = '';

  /// 签字
  String approvalUser = '';
  /// 初始页面设置 [initPage] 0: 1：3
  int initPage = 0;

  /// 流程节点数据
  List<ProcessNodesEntity> processList = [];

  /// 是否是审批
  bool isApprove = Get.arguments['isApprove'] as bool;

  /// 是否是从我的任务进入
  bool isMeTask = Get.arguments['isMeTask'] as bool;
  String id =  Get.arguments['id'] as String;
  @override
  void onInit() {
    super.onInit();
    getProcessListData();
  }

  /// 获取流程列表数据
  void getProcessListData() async {
    // 获取流程列表接口
    final res = await HttpUtil().generateGetRequest<List<ProcessNodesEntity>>(
      Interface.getApprovalHistory,
      queryParameters: {'businessId': id},//计划id
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        processList = result.data!;
      }
      update();
    });
  }

  /// 输入回调
  void onOpinion(String value) {
    approvalOpinions = value;
    update();
  }

  /// 签字回调
  void onSign(String url) {
    approvalUser = url;
  }

  /// 操作回调
  /// [type]1:通过 2:驳回 驳回即为不通过
  Future<void> onOperation(int type) async {
    // 添加分配（为部门分配任务）
    final res = await HttpUtil().generatePostRequest(
      Interface.educationStartApproval,
      data: {
        'businessId': id, //业务数据id
        'reject': type == 1 ? false : true, //是否驳回
        'comments': approvalOpinions, //	意见
        'code': 1,
        'signatureAddress': approvalUser //签名地址
      },
    ).execute();
    res.onSuccess((result) {
      Get.back(closeOverlays: true);
      if (isMeTask) {
        //我的任务里的列表（培训计划(ing)）
        final TrainIngProgramController riskController = Get.find();
        riskController.globalKey.currentState?.onRefresh();
      } else {
        // 刷新列表
        final TrainProgramController riskController = Get.find();
        riskController.globalKey.currentState?.onRefresh();
      }
    });
  }
}

